<template>
  <div>
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="$emit('close')"
      @click-right="onConfirm"
    />

    <!-- 输入框 -->
    <div class="field-wrap">
      <van-field
        v-model.trim="localName"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
    <!-- /输入框 -->
  </div>
</template>

<script>
import { Toast } from 'vant'
import { updatename } from '@/api/user.js'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      localName: ''
    }
  },
  created() {
    this.localName = this.value
  },

  methods: {
    async onConfirm() {
      // console.log(123)
      if (this.localName === '') return Toast('请填写昵称')
      try {
        const res = await updatename({
          name: this.localName
        })
        console.log(res)
        Toast.success('cg')
        this.$emit('close')
        this.$emit('input', this.localName)
      } catch (err) {
        if (err.response && err.response.status === 409) {
          Toast.fail('cz')
          return
        }
        Toast.fail('sb')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.field-wrap {
  padding: 20px;
}
</style>
